﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoginManager
{
    private static LoginManager instance = new LoginManager();
    public static LoginManager Instance => instance;

    private LoginData loginData;
    public LoginData LoginData => loginData;

    private RegisterData registerData;

    public List<ServerData> ServerData
    {
        get;
        private set;
    }

    private LoginManager() 
    {
        LoadLoginData();
        LoadRegisterData();
        LoadServerData();
    }

    public void LoadServerData()
    {
        ServerData = JsonManager.Instance.LoadData<List<ServerData>>("ServerInfo");
    }

    public void SaveLoginData()
    {
        JsonManager.Instance.SaveData(loginData, "LoginData");
    }

    public void LoadLoginData()
    {
        loginData = JsonManager.Instance.LoadData<LoginData>("LoginData");
    }

    public void ClearLoginData()
    {
        loginData = new LoginData();
    }

    #region 注册
    public void SaveRegisterData()
    {
        JsonManager.Instance.SaveData(registerData, "RegisterData");
    }

    public void LoadRegisterData()
    {
        registerData = JsonManager.Instance.LoadData<RegisterData>("RegisterData");
    }

    public bool RegisterUser(string name, string password)
    {
        if (registerData.registerDic.ContainsKey(name))
        {
            return false;
        }

        registerData.registerDic.Add(name, password);

        SaveRegisterData();

        return true;
    }

    public bool VerifyInfo(string name, string password)
    {
        //账户密码是否正确
        if (registerData.registerDic.ContainsKey(name))
        {
            if (registerData.registerDic[name] == password)
            {
                return true;
            }
        }

        return false;
    }


    #endregion

}
